package com.feeyo.groundservice.base_util

import android.animation.ObjectAnimator
import android.view.View
import android.view.animation.Animation
import android.view.animation.CycleInterpolator
import android.view.animation.LinearInterpolator
import android.view.animation.RotateAnimation
import android.view.animation.TranslateAnimation

/**
 *作者：70893 on 2025-09-17
 *描述：View动画类
 */


/**旋转动画
 * @param time ms
 * **/
fun View.rotateAnimation(time: Long = 300) {
    RotateAnimation(
        0f, 359f,
        Animation.RELATIVE_TO_SELF, 0.5f,
        Animation.RELATIVE_TO_SELF, 0.5f
    ).let {
        it.duration = time
        it.repeatCount = 20//循环次数
        it.fillAfter = true//设置保持动画最后的状态
        it.interpolator = LinearInterpolator()
        startAnimation(it)
    }
}


/**
 * 晃动动画
 * **/
fun View.shakeAnimation() = this.startAnimation(shakeAnimations())


/**
 * 晃动动画
 * @param counts time时间内晃动多少下
 * @param time 时间 ms
 * @return
 */
fun shakeAnimations(counts: Int = 5, time: Int = 1000): Animation =
    TranslateAnimation(0f, 10f, 0f, 0f).apply {
        interpolator = CycleInterpolator(counts.toFloat())
        duration = time.toLong()
    }

/**
 * 渐隐渐现动画 - 隐藏
 *
 * @param view
 * @param duration
 */
fun View.graduallyHide(duration: Long) {
    ObjectAnimator.ofFloat(this, "alpha", 0.9f, 0f).apply {
        this.duration = duration
        start()
    }
}

/**
 * 渐隐渐现动画 - 显示
 *
 * @param view
 * @param duration
 */

fun View.graduallyDisplay(duration: Long) {
    ObjectAnimator.ofFloat(this, "alpha", 0f, 0.9f).apply {
        this.duration = duration
        start()
    }
}
